package com.hzcx.app.simplechat.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.ui.friend.event.AddFriendEvent;
import com.hzcx.app.simplechat.ui.home.contract.AddFriendContract;
import com.hzcx.app.simplechat.ui.home.dialog.AddFriendDialog;
import com.hzcx.app.simplechat.ui.home.presenter.AddFriendPresenter;
import com.hzcx.app.simplechat.ui.publicui.bean.HxUserInfoBean;
import com.hzcx.app.simplechat.ui.user.UserRemarkActivity;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.ui.user.enums.SexEnum;
import com.hzcx.app.simplechat.ui.user.event.UserRemarkEvent;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.view.imageview.RoundImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加好友界面
 */
public class AddFriendActivity extends BaseActivity<AddFriendPresenter> implements AddFriendContract.View {

    public static final String INTENT_USER_ID = "INTENT_USER_ID";
    public static final String INTENT_TYPE = "INTENT_TYPE";
    public static final String PROTECT_GROUP_MEMBER = "PROTECT_GROUP_MEMBER";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.iv_head)
    RoundImageView ivHead;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.tv_remark_name)
    TextView tvRemarkName;
    @BindView(R.id.iv_sex)
    ImageView ivSex;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_user_id)
    TextView tvUserId;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.ll_sign)
    LinearLayout llSign;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.cons_main)
    ConstraintLayout consMain;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_banned)
    TextView tvBanned;

    private UserInfoBean userInfoBean;
    private boolean isFriend = false;

    @Override
    public int getLayout() {
        return R.layout.activity_addfriend;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AddFriendPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("个人信息");
        ivTitleRight.setImageResource(R.mipmap.ic_icon_title_more);
        if (getIntent().getBooleanExtra(PROTECT_GROUP_MEMBER, false)) {
            tvAdd.setVisibility(View.GONE);
            tvUserId.setVisibility(View.GONE);
            tvCity.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {
        mPresenter.getUserInfo(this, getIntent().getIntExtra(INTENT_USER_ID, 0));
    }

    @OnClick({R.id.tv_add, R.id.tv_remark})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                sendAdd();
                break;

            case R.id.tv_remark: // 备注及标签
                startActivity(new Intent(this, UserRemarkActivity.class)
                        .putExtra(UserRemarkActivity.INTENT_USER_INFO, userInfoBean));
                break;
        }
    }

    /**
     * 发送添加好友请求
     */
    private void sendAdd() {
        if (!isFriend) {
            // 不是好友，点击发送好友请求
            AddFriendDialog addFriendDialog = new AddFriendDialog(this, remark ->
                    mPresenter.sendAddFriendRequest(AddFriendActivity.this, getIntent().getIntExtra(INTENT_USER_ID, 0), remark
                            , getIntent().getIntExtra(INTENT_TYPE, 1)));
            addFriendDialog.show();
        } else {
            // 是好友，直接聊天
            HxUserInfoBean hxUserInfoBean = new HxUserInfoBean();
            hxUserInfoBean.setHxusername(userInfoBean.getHxusername());
            hxUserInfoBean.setAvatar(userInfoBean.getAvatar());
            hxUserInfoBean.setBy_remarks_name(userInfoBean.getBy_remarks_name());
            hxUserInfoBean.setMember_id(userInfoBean.getMember_id());
            hxUserInfoBean.setNickname(userInfoBean.getNickname());
            hxUserInfoBean.setRemarks_name(userInfoBean.getRemarks_name());
            hxUserInfoBean.setMyUserId(UserInfoUtil.getUserId());
            hxUserInfoBean.setIs_assistant(userInfoBean.getIs_assistant());
            ChatUtil.getInstance().toChatUi(this, hxUserInfoBean);
        }
    }

    /**
     * 用户信息获取成功
     *
     * @param data
     */
    @Override
    public void userInfoResult(UserInfoBean data) {
        userInfoBean = data;
        GlideUtils.loadImg(this, data.getAvatar(), ivHead);

        if (EmptyUtils.isEmpty(data.getRemarks_name())) {
            tvRemarkName.setText(data.getNickname());
            tvNickName.setVisibility(View.GONE);
        } else {
            tvRemarkName.setText(data.getRemarks_name());
            tvNickName.setText("昵称：" + data.getNickname());
            tvNickName.setVisibility(View.VISIBLE);
        }

        if (UserInfoUtil.getUserSex().equals(SexEnum.SEX_GRIL)) {
            ivSex.setImageResource(R.mipmap.ic_icon_sex_gril);
            ivSex.setVisibility(View.VISIBLE);
        } else if (UserInfoUtil.getUserSex().equals(SexEnum.SEX_BOY)) {
            ivSex.setImageResource(R.mipmap.ic_icon_sex_boy);
            ivSex.setVisibility(View.VISIBLE);
        } else {
            ivSex.setVisibility(View.GONE);
        }
        tvUserId.setText(getResources().getString(R.string.app_name) + "号：" + data.getChatnumber());
        tvCity.setText(data.getArea());
        tvSign.setText("个性签名：" + data.getBio());

        consMain.setVisibility(View.VISIBLE);

        // 如果对方被封禁，则无法加好友
        if (data.getIs_banned() == 1) {
            tvAdd.setBackgroundResource(R.drawable.btn_send_code_bg_normal);
            tvAdd.setTextColor(getResources().getColor(R.color.color_282F3E));
            tvAdd.setEnabled(false);
            tvBanned.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 加好友请求发送成功
     */
    @Override
    public void sendSuccess() {
        showError("发送成功");
//        finish();
    }

    /**
     * 用户备注名设置成功
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setRemarkNameSuccess(UserRemarkEvent event) {
        userInfoBean = event.getBean();
        if (EmptyUtils.isEmpty(userInfoBean.getRemarks_name())) {
            tvRemarkName.setText(userInfoBean.getNickname());
            tvNickName.setVisibility(View.GONE);
        } else {
            tvRemarkName.setText(userInfoBean.getRemarks_name());
            tvNickName.setText("昵称：" + userInfoBean.getNickname());
            tvNickName.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 对方同意好友成功，修改逻辑
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void addFriendSuccess(AddFriendEvent event) {
        if (event.getId() == userInfoBean.getMember_id()) {
            // 对方同意好友请求，修改状态
            isFriend = true;
            tvAdd.setText("聊天");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
